<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$robot_setting=uni_setting('robot');
if(!$robot_setting['api_key']){
	exi('未关联机器人！','error');
}
$tabs=array(
	array('op'=>'list','title'=>'语料库管理'),
	array('op'=>'edit','title'=>'添加问题'),
	array('op'=>'robot_edit','title'=>'机器人信息'),
);
set_html('tabs',$tabs);
$api_url='http://ai.jinyunweb.com/api/manage.php?api_key='.$robot_setting['api_key'];
if($_RQ['op']=='list'){
	$query=array('do'=>'list');
	if($_RQ['page']){
		$query['page']=intval($_RQ['page']);
	}
	if($_RQ['answer']){
		$query['answer']=intval($_RQ['answer']);
	}
	$api_url.='&'.http_build_query($query);
	//exi($api_url,'error');
	$result=model('core/http')->ihttp_request($api_url);
	$result=@json_decode($result['content'],true);
	exi($result['message'],$result['type']);
}
if($_RQ['op']=='edit'){
	$query=array('do'=>'edit');
	if($_RQ['id']){
		$query['id']=intval($_RQ['id']);
	}
	
	$api_url.='&'.http_build_query($query);
	$result=model('core/http')->ihttp_request($api_url);
	$result=@json_decode($result['content'],true);
	exi($result['message'],$result['type']);
}
if($_RQ['op']=='post'){
	$query=array('do'=>'post');
	
	$api_url.='&'.http_build_query($query);
	$post=get_postdata();
	$post=array('postdata'=>base64_encode(ijson_encode($post)));
	$result=model('core/http')->ihttp_request($api_url,$post);
	$result=@json_decode($result['content'],true);
	if($result['type']=='success'){
		exi('添加成功！','','goback');
	}else{
		exi('添加失败！','error');
	}
	
}
if($_RQ['op']=='robot_edit'){
	$query=array('do'=>'robot_edit');

	$api_url.='&'.http_build_query($query);
	$result=model('core/http')->ihttp_request($api_url);
	$result=@json_decode($result['content'],true);
	exi($result['message'],$result['type']);
}
if($_RQ['op']=='robot_post'){
	$query=array('do'=>'robot_post');
	
	$api_url.='&'.http_build_query($query);
	$post=get_postdata();
	$post=array('postdata'=>base64_encode(ijson_encode($post)));
	$result=model('core/http')->ihttp_request($api_url,$post);
	$result=@json_decode($result['content'],true);
	if($result['type']=='success'){
		exi('设置成功！','','goback');
	}else{
		exi('设置失败！','error');
	}
}
if($_RQ['op']=='del'){
	$query=array('do'=>'del');
	if($_RQ['id']){
		$query['id']=intval($_RQ['id']);
	}
	if($_RQ['del']){
		$query['del']=trim($_RQ['del']);
	}
	$api_url.='&'.http_build_query($query);
	$result=model('core/http')->ihttp_request($api_url);
	$result=@json_decode($result['content'],true);
	exi($result['message'],$result['type']);
}